import edcExcelViewTableInsertCombineRow from "./edc-excelView-tableInsertCombineRow";
import edcExcelViewTableInsertRow from "./edc-excelView-tableInsertRow";
import edcExcelViewPaste from "./edc-excelView-paste";
import CellType from '../../../excel/celltypes';
import edcExcelViewTableDeleteRow from "./edc-excelView-tableDeleteRow";
import edcExcelViewTableDeleteValue from "./edc-excelView-deleteValue"
import edcExcelViewTableDeleteValues from "./edc-excelView-deleteValues"
import edcExcelViewCusTableInsertRow from "./edc-excelView-custableInsertRow";
import edcExcelViewCusTableDeleteRow from "./edc-excelView-custableDeleteRow";
import edcExcelViewCusTableInsertCol from "./edc-excelView-custableInsertCol";
import edcExcelViewCusTableDeleteCol from "./edc-excelView-custableDeleteCol";
import edcExcelViewDeletePic from './edc-excelView-deletePic'
import edcExcelViewPaste1 from "./edc-excelView-paste1";

import { isEqual } from 'lodash';
export const splitArray = (array, index) => {
    const array1 = [];
    const array2 = []
    array.forEach((value, i) => {
        if (i > index) {
            array2.push(value);
        } else {
            array1.push(value);
        }
    });
    return [array1, array2];
}
export const getNewValue = (array, rowIndex, spanKeys,rowCount=1) => {
    const [array1, array2] = splitArray(array, rowIndex);
    const currentValue = array[rowIndex];
    const newValue = [];
    for(var i = 0; i<rowCount; i++){
        newValue.push({ detail_index: rowIndex+1+i+1 })
    }
    spanKeys.forEach(key => {
        newValue.map(newValueObj=>{
            newValueObj[key.name] = currentValue[key.name];
        })
    });
    return [...array1, ...newValue, ...array2.map(d => {
        d.detail_index = d.detail_index + rowCount
        return d;
    })]
}
export const removeAllSpan = (sheet, table) => {
    const { spanKeys = [] } = table;
    const { row, col, rowCount, colCount } = table.dataRange();
    const jndexs = spanKeys.map(d => d.index);
    for (let index = 0; index < rowCount; index++) {
        for (let jndex = 0; jndex < colCount; jndex++) {
            if (jndexs.indexOf(jndex) > -1) {
                sheet.removeSpan(row + index, col + jndex);
            }
        }
    }
}
export const sortRecords = (records, spanKeys) => {
    if (spanKeys.length === 0) {
        return records;
    }
    const newRecords = [];
    const recordMap = {};
    const [spanKey, ...rightKeys] = spanKeys;
    const spanKeyName = spanKey.name;
    records.forEach(record => {
        const key = JSON.stringify(record[spanKeyName]);
        if (newRecords.indexOf(key) > -1) {
            recordMap[key].push(record);
        } else {
            newRecords.push(key);
            recordMap[key] = [record];
        }
    });

    const result = [];
    newRecords.forEach(key => {
        const currentRecords = sortRecords(recordMap[key], rightKeys);
        currentRecords.forEach(record => {
            result.push(record);
        });
    });
    return result;
}
export const isEditableArea = (cell) => {
    const cellType = cell.cellType();
    const WeaInputCellType = CellType('WeaInputCellType');
    const WeaCheckboxCellType = CellType('WeaCheckboxCellType');
    const WeaBrowserCellType = CellType('WeaBrowserCellType');
    const WeaSelectCellType = CellType('WeaSelectCellType');
    const WeaDatePickerCellType = CellType('WeaDatePickerCellType');
    const WeaTimePickerCellType = CellType('WeaTimePickerCellType');
    const WeaPicCellType = CellType('WeaPicCellType');

    return cellType instanceof WeaInputCellType ||
        cellType instanceof WeaCheckboxCellType ||
        cellType instanceof WeaBrowserCellType ||
        cellType instanceof WeaSelectCellType ||
        cellType instanceof WeaDatePickerCellType||
        cellType instanceof WeaTimePickerCellType||
        cellType instanceof WeaPicCellType;
}
export const isSameType = (cell, sourceCell) => {
    const cellType = cell.cellType();
    const sourceCellType = sourceCell.cellType();
    return (cellType && sourceCellType && isEqual(cellType.fieldType, sourceCellType.fieldType)) ||
        (cellType && cellType.fieldType && cellType.fieldType[0] && cellType.fieldType[0] === 'input');
}
export const toCells = (range) => {
    const cells = [];
    const { row, col, rowCount, colCount, sheet } = range;
    for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
        const currentRow = row + rowIndex;
        for (let colIndex = 0; colIndex < colCount; colIndex++) {
            const currentCol = col + colIndex;
            const span = sheet.getSpan(currentRow, currentCol);
            if (!span || span.row === currentRow && span.col === currentCol) {
                cells.push(sheet.getCell(currentRow, currentCol));
            }
        }
    }
    return cells;
}
function r(a) {
    var b, c, d, e, f, g = [];
    if (a) {
        for (b = '"',
            c = !1,
            d = 0; d < a.length; d++)
            e = d > 0 ? a[d - 1] : "",
                f = a[d],
                f === b ? (c = !c,
                    g.push(b)) : c || "\n" !== f || "\r" === e ? g.push(f) : g.push("\r\n");
        return g.join("")
    }
    return ""
}
export const register = (commandManager, store) => {
    commandManager.register('edc.excelView.tableInsertCombineRow', edcExcelViewTableInsertCombineRow);
    commandManager.register('edc.excelView.tableInsertRow', edcExcelViewTableInsertRow);
    commandManager.register('edc.excelView.paste', edcExcelViewPaste);
    commandManager.register('edc.excelView.tableDeleteRow', edcExcelViewTableDeleteRow);
    commandManager.register('edc.excelView.clearValues',edcExcelViewTableDeleteValues);
    commandManager.register('edc.excelView.delete',edcExcelViewTableDeleteValue);
    commandManager.register('edc.excelView.custableInsertRow', edcExcelViewCusTableInsertRow);
    commandManager.register('edc.excelView.custableDeleteRow', edcExcelViewCusTableDeleteRow);
    commandManager.register('edc.excelView.custableInsertCol', edcExcelViewCusTableInsertCol);
    commandManager.register('edc.excelView.custableDeleteCol', edcExcelViewCusTableDeleteCol);
    //commandManager.register('edc.excelView.paste1', edcExcelViewPaste1, GC.Spread.Commands.Key.v, true)
    commandManager.register('edc.excelView.deletePic', edcExcelViewDeletePic);
    
    commandManager.paste = undefined;
    store.workbook.uu.KM.removeEventListener('paste', store.workbook.uu.H4);
    store.workbook.uu.KM.addEventListener('paste', (event) => {
        try {
            const sheet = commandManager.xc.getActiveSheet();
            const { clipboardData } = event
            const clipboardManager = commandManager.xc.uu;
            if (clipboardData) {
                const html = clipboardData.getData("text/html");
                const textarea = clipboardManager.I4();
                textarea.innerHTML = html;
                const text = html ? clipboardManager.A$a(textarea, !0) : r(clipboardData.getData("text/plain"));
                textarea.innerHTML = "";
                const pasteEvent = sheet.xx(text);
                pasteEvent.cmd = "clipboardPaste";
                pasteEvent.sheetName = sheet.name();
                pasteEvent.clipboardHtml = html;
                store.clipboardPasted(pasteEvent)
            }
        } catch (e) {

        }
    })

    //清空源生事件
    commandManager.setShortcutKey(null,window.GC.Spread.Commands.Key.del, false, false, false, false);
    //绑定自定义事件
    commandManager.setShortcutKey('edc.excelView.delete',window.GC.Spread.Commands.Key.del, false, false, false, false);
}
